package com.dc;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: KEYD
 * @Date: 2019/12/25 18:19
 * @Desc: 微服务网关
 */
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
@MapperScan(basePackages = "com.dc.zuul.mapper")
@EnableFeignClients
//@EnableApolloConfig
public class AppZuul {

    public static void main(String[] args) {
        SpringApplication.run(AppZuul.class, args);
    }

    //// 获取ApolloConfig的配置文件
    //@ApolloConfig
    //private Config appConfig;

    //@Component
    //@Primary
    //class DocumentationConfig implements SwaggerResourcesProvider {
    //    @Override
    //    public List<SwaggerResource> get() {
    //        return apolloResources();
    //    }

    //    /**
    //     * 获取swaggerDocument配置
    //     *
    //     * @return
    //     */
    //    private String swaggerDocument() {
    //        String property = appConfig.getProperty("dc.zuul.swagger.document", ""); //value  default
    //        return property;
    //    }

    //    /**
    //     * 从阿波罗服务器中获取resources
    //     *
    //     * @return
    //     */
    //    private List<SwaggerResource> apolloResources() {
    //        List resources = new ArrayList<>();
    //        // app-itmayiedu-order
    //        // 网关使用服务别名获取远程服务的SwaggerApi
    //        String swaggerDocJson = swaggerDocument();
    //        JSONArray jsonArray = JSONArray.parseArray(swaggerDocJson);
    //        for (Object object : jsonArray) {
    //            JSONObject jsonObject = (JSONObject) object;
    //            String name = jsonObject.getString("name");
    //            String location = jsonObject.getString("location");
    //            String version = jsonObject.getString("version");
    //            resources.add(swaggerResource(name, location, version));
    //        }
    //        return resources;
    //    }


    //    private SwaggerResource swaggerResource(String name, String location, String version) {
    //        SwaggerResource swaggerResource = new SwaggerResource();
    //        swaggerResource.setName(name);
    //        swaggerResource.setLocation(location);
    //        swaggerResource.setSwaggerVersion(version);
    //        return swaggerResource;
    //    }
    //}

    // 添加文档来源  写死的
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList<>();
            // app-dc-member 注册的服务别名，获取远程服务的swagger接口  swagger文档     写成 json 格式
            resources.add(swaggerResource("app-dc-member", "/app-dc-member/v2/api-docs", "1.0"));
            resources.add(swaggerResource("app-dc-weixin", "/app-dc-weixin/v2/api-docs", "1.0"));
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }
}

